﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;

namespace QLTT.ViewModels
{
    class AllUserViewModel : WorkspaceViewModel
    {
        private ObservableCollection<UserViewModel> _lstUserViewModel;
        private icanDataContext _dataContext;
        private DelegateCommand _addCommand;
        private DelegateCommand _deleteCommand;
        private IQueryable<UserViewModel> _query;
        private string _userName;
        public AllUserViewModel(icanDataContext dataContext)
        {
            _lstUserViewModel = new ObservableCollection<UserViewModel>();
            _dataContext = dataContext;
            base.DisplayName = Properties.Resources.Base_DisplayName_AllUser; 
        }
        public AllUserViewModel(icanDataContext dataContext, bool isGetAll)
        {
            _lstUserViewModel = new ObservableCollection<UserViewModel>();
            _dataContext = dataContext;
            if (isGetAll)
            {
                _query = from user in _dataContext.Users orderby user.ID descending select new UserViewModel(_dataContext, user);
                this.NumRow = _query.Count();
                this.CurPage = 1;
                this.UpdateList();
            }
            base.DisplayName = Properties.Resources.Base_DisplayName_AllUser;
        }
        public string Name
        {
            get { return _userName; }
            set { _userName = value; }
        }

        public ObservableCollection<UserViewModel> AllUsers
        {
            get { return _lstUserViewModel; }
            set
            {
                _lstUserViewModel = value;
                base.OnPropertyChanged("AllUsers");
            }
        }
       
        public ICommand AddCommand
        {
            get
            {
                if (_addCommand == null)
                {
                    _addCommand = new DelegateCommand(AddUser);
                }
                return _addCommand;
            }
        }
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    _deleteCommand = new DelegateCommand(DeleteUser, CanDeleteUser);
                }
                return _deleteCommand;
            }
        }

        
        public void AddUser()
        {
            UserViewModel ws = new UserViewModel(_dataContext);
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, ws);
        }

        private void DeleteUser()
        {
            foreach (UserViewModel lvm in AllUsers)
            {
                lvm.Delete();
            }
            this.NumRow = _query.Count();
            this.UpdateList();

        }
        private bool CanDeleteUser()
        {
            UserViewModel lVM = AllUsers.FirstOrDefault(l => l.IsSelected == true && l.ID != ViewModelBase.CurrentUser.ID);
            if (lVM == null)
                return false;
            return lVM.CanDelete;
        }
        [MediatorLib.MediatorMessageSink(Messages.ListUserUpdated, ParameterType = typeof(User))]
        public void UpdateListUser(User user)
        {
            this.NumRow = _query.Count();
            this.UpdateList();
        }

        public override void UpdateList()
        {
            List<UserViewModel> lstUsers = _query.Skip((this.CurPage - 1) * this.RowsPerPage).Take(this.RowsPerPage).ToList();
            AllUsers = new ObservableCollection<UserViewModel>(lstUsers);
            base.UpdateList();
        }
    }
}
